<?php

class ClientController extends Controller {

    public function actionFormLogin() {
        $lang = $_GET['lang'];
        if (Yii::app()->session['client'])
            $this->redirect("index.php?lang={$lang}&r=Jobseeker/List");
        $error = isset($_GET['error'])?$_GET['error']:0;
        $this->render('login', array('lang' => $lang, 'error' => $error));
    }

    public function actionFormRegister() {
        $lang = $_GET['lang'];
        $this->render('register', array('lang' => $lang));
    }

    public function actionRegister() {
        $lang = $_GET['lang'];
        if(isset($_POST)){
            $ClientID = $_POST['ClientID'];
            $ContactName = $_POST['ContactName'];
            $CompanyName = $_POST['CompanyName'];
            $Address = $_POST['Address'];
            $ProvinceID = $_POST['Province'];
            $ZipCode = $_POST['ZipCode'];
            $Email = isset($_POST['Email'])?$_POST['Email']:'';
            $Phone = $_POST['Phone'];
            $Fax = $_POST['Fax'];
            $Mobile = $_POST['Mobile'];
            
             if ($ClientID == '') {
                Client::insertCustomer($ContactName, $CompanyName, $Address, $ProvinceID, $ZipCode, $Email, $Phone, $Fax, $Mobile, $lang, 'I');
            } else {
                Client::updateCustomer($ClientID, $ContactName, $CompanyName, $Address, $ProvinceID, $ZipCode, $Phone, $Fax, $Mobile);
            }
        }
        $this->render('thankyou', array('lang' => $lang));
    }
    
    public function actionActivate(){
        $lang = isset($_GET['lang'])?$_GET['lang']:'th';
        if(isset($_GET['ActivateKey'])){
            $aKey = $_GET['ActivateKey'];
            $clientID = Client::getClientIDByAKey($aKey);
            $this->render('setpassword', array(
                'lang' => $lang,
                'clientID' => $clientID,
                'titleName' => 'New Password'
                ));
        }else{
            $this->redirect("index.php?lang={$lang}&r=Client/FormLogin");
        }
    }
    
    public function actionRetrievePassword(){
        $lang = isset($_GET['lang'])?$_GET['lang']:'th';
        if(isset($_GET['RetrieveKey'])){
            $rKey = $_GET['RetrieveKey'];
            $clientID = Client::getClientIDByRKey($rKey);
            $this->render('setpassword', array(
                'lang' => $lang,
                'clientID' => $clientID,
                'titleName' => 'Retrieve Password'
                ));
        }else{
            $this->redirect("index.php?lang={$lang}&r=Client/FormLogin");
        }
    }
    
    public function actionRepassword(){
        $lang = 'th';
        if(isset($_GET['email'])&&isset($_GET['lang'])){
            $lang = $_GET['lang'];
            $email = $_GET['email'];
            echo Client::Repassword($email, $lang);
        }else{
            $this->redirect("index.php?lang={$lang}&r=Client/FormLogin");
        }
        
    }

    public function actionLogin() {
        $lang = $_GET['lang'];
        if(isset($_POST['Email']) && isset($_POST['Password'])){
            $Email = $_POST['Email'];
            $Password = $_POST['Password'];
            if(Client::checkLogin($Email, $Password)){
                Yii::app()->session['client'] = true;
                Yii::app()->session['clientName'] = 'Company';
                $this->redirect("index.php?lang={$lang}&r=Jobseeker/List");
            } else {
                $this->redirect("index.php?lang={$lang}&r=Client/FormLogin&error=1");
            }
        }
        $this->redirect("index.php?lang={$lang}&r=Client/FormLogin");
        
    }

    public function actionLogout() {
        unset(Yii::app()->session['client']);
        unset(Yii::app()->session['clientName']);
        $this->redirect('index.php');
    }
    
    public function actionSetPassword(){
        if(isset($_POST)){
            $lang = $_POST['lang'];
            $client = $_POST['ClientID'];
            $password = $_POST['Password'];
            Client::setPassword($client, $password);
            $this->redirect("index.php?lang={$lang}&r=Client/FormLogin");
            //$this->render('', array('lang' => $lang));
        }
    }

}

?>
